package org.example.controller.admin;

import com.google.protobuf.Api;
import lombok.extern.slf4j.Slf4j;
import org.example.common.ApiResponse;
import org.example.entity.system.User;
import org.example.service.impl.PermissionServiceImpl;
import org.example.service.impl.RoleServiceImpl;
import org.example.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * @Description
 * @Author 魏锴
 * @Date 2024/1/11
 */

@Slf4j
@Controller
@ResponseBody
@RequestMapping("/admin/user")
public class userController {
    @Autowired
    UserServiceImpl userService;
    @Autowired
    RoleServiceImpl roleService;
    @Autowired
    PermissionServiceImpl permissionService;

    @PostMapping("/create")
    public ApiResponse insertUser(User userVO) {
        try {
            if (userService.insertUser(userVO)) return ApiResponse.ok("创建成功");
            else return ApiResponse.failure("创建失败");
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failure("创建失败");
        }
    }

    @PostMapping("/update")
    public ApiResponse updateUser(User userVO) {
        try {
            if (userService.updateUser(userVO)) return ApiResponse.ok("修改成功");
            else return ApiResponse.failure("修改失败");
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failure("修改失败");
        }
    }

    @PostMapping("/delete/{id}")
    public ApiResponse deleteUser(@PathVariable String id) {
        try {
            if (userService.deleteUser(Integer.parseInt(id))) return ApiResponse.ok("删除成功");
            else return ApiResponse.failure("删除失败");
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failure("删除失败");
        }
    }

    @GetMapping("/list")
    public ApiResponse getUserList() {
        try {
            return ApiResponse.ok("获取成功", userService.getUserList());
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failure("获取失败");
        }
    }
}
